/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.zeos.bean;

import br.zeos.ZDataSource;
import br.zeos.util.POG;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;

/**
 *
 * @author virtual
 */
public class zImage extends javax.swing.JPanel implements ActionListener {

    private ZDataSource ds = null;
    private String FieldName = "";
    private File arquivo = null;
    private boolean enabled = true;
    public void setEnabled(boolean value){
        this.enabled = value;
        this.jButton1.setEnabled(this.enabled);
    }
    public boolean isEnabled(){
        return this.enabled;
    }
    
    public ZDataSource getDataSource() {
        return ds;
    }

    public void setDataSource(ZDataSource ds) {
        this.ds = ds;
    }

    public String getFieldName() {
        return FieldName;
    }

    public void setFieldName(String FieldName) {
        this.FieldName = FieldName;
        this.ds.addUpdateListener(this);
    }

    /**
     * Creates new form zImage
     */
    public zImage() {
        initComponents();        
    }

    private void UpdateData() {
        try {
            HashMap<String, String> linha = this.ds.getLine();
            if (linha == null) {
                return;
            }
            linha.put(FieldName.toUpperCase(), POG.fileToSQL(arquivo));
            this.ds.setLinha(linha);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        imagePane1 = new br.zeos.util.ImagePane();
        jButton1 = new javax.swing.JButton();

        imagePane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout imagePane1Layout = new javax.swing.GroupLayout(imagePane1);
        imagePane1.setLayout(imagePane1Layout);
        imagePane1Layout.setHorizontalGroup(
            imagePane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        imagePane1Layout.setVerticalGroup(
            imagePane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 227, Short.MAX_VALUE)
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/006.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(imagePane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(imagePane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.arquivo = POG.selectFile("", new String[]{"jpg", "png", "bmp"}, "Arquivos de imagem");
        if (arquivo == null) {
            return;
        }
        String hexa = POG.fileToSQL(this.arquivo);
        byte[] img = POG.sQLToFile(hexa);
        this.imagePane1.setImage(img);
        this.UpdateData();
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private br.zeos.util.ImagePane imagePane1;
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent ae) {
        String valor = this.ds.getLine().get(FieldName.toUpperCase());
        if (valor == null) {
//            return;
        }
//        if (valor.isEmpty()) {
////            return;
//        }
//        this.imagePane1.setImage(POG.sQLToFile(valor));
        byte[] imagemDaMordida = POG.sQLToFile(valor);
        this.imagePane1.setImage(imagemDaMordida);
    }
}
